clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "$datapath\moments_dta"

use "$datapath\data_task_panel_cluster_Feb24.dta", clear
// merge 1:1 KIDID id ts_id using age_index, nogen

foreach stat in mean se N {
	gen `stat'_task=task
}
preserve
collapse (mean) mean_task (semean) se_task (count) N_task, by(ts_id)
gen lb_95 = mean_task - 1.96*se_task
gen ub_95 = mean_task + 1.96*se_task
save alltask_rate, replace
restore

foreach i in l f2 c2 g{
preserve
collapse (mean) mean_task (semean) se_task (count) N_task if level`i' != ., by(level`i')
gen lb_95 = mean_task - 1.96*se_task
gen ub_95 = mean_task + 1.96*se_task
save level`i'_rate, replace
restore

}

forval i = 1/5{
	forval j = 2/11{
		gen ind_artl`i'_l`j' = 1 if artl`i' == 1 & levell == `j'
		bys KIDID (levell): carryforward ind_artl`i'_l`j', replace
	}
}

tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artl_rate_pre1, replace

// post `table' ("Condtional on previous level") (0) (0) (0) (0) (0) (0) (0)
forval i = 2/10{
	local j = `i'+1
	forval k = 1/5{		
		forval n = 1/5{
			mean artl`n' if ind_artl`k'_l`i' == 1 & levell == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artl`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'



tempfile artl_rate
tempname table
postfile `table' str60(Dataset) l_condition l_focus mean se lb ub N using artl_rate_pre2, replace

// post `table' ("Condtional on previous 2 level") (0) (0) (0) (0) (0) (0) (0)
forval i = 2/9{
	local j = `i'+ 2
	forval k = 1/5{		
		forval n = 1/5{
			mean artl`n' if ind_artl`k'_l`i' == 1 & levell == `j'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artl`n'") (`i') (`j') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}

postclose `table'


tempfile artl_rate
tempname table
postfile `table' str60(Dataset) task_condition l_focus mean se lb ub N using artl_rate_pretask, replace


// post `table' ("Condtional previous task") (0) (0) (0) (0) (0) (0) (0)
// post `table' ("l_cdt now is the task controled") (0) (0) (0) (0) (0) (0) (0)

forval i = 2/11{
// 	local j = `i'+1
	forval k = 1/4{
		local j = `k' + 1
		forval n = `j'/5{
			mean artl`n' if ind_artl`k'_l`i' == 1 & levell == `i'
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' ("artl`n'") (`k') (`i') (`m') (`s') (`lb') (`ub') (`N')
		}
	}
}



postclose `table'


tempfile artl_rate
tempname table
postfile `table' level artl mean se lb ub N using artl1-5_rate, replace


// post `table' ("Condtional previous task") (0) (0) (0) (0) (0) (0) (0)
// post `table' ("l_cdt now is the task controled") (0) (0) (0) (0) (0) (0) (0)

forval i = 2/11{
	forval n = 1/5{
			mean artl`n' if levell == `i' & indl == 1
			local m = r(table)[1,1]
			local s = r(table)[2,1]
			local lb = r(table)[5,1]
			local ub = r(table)[6,1]
			local N = e(N)
			post `table' (`i') (`n') (`m') (`s') (`lb') (`ub') (`N')
	}
}



postclose `table'

use "$datapath/Eleanor/Control Groups/controlgroup.dta", clear
foreach i in old new{
	gen ub_`i'=mean_`i'+1.96*se_`i'
	gen lb_`i'=mean_`i'-1.96*se_`i'

}
label var mean_old "Enrolled > 1 Month"
label var mean_new "Enrolled < 1 Month"
save enroll_1mo, replace


